using System;
using System.ComponentModel;
using System.Windows.Forms;
using Syncfusion.Windows.Forms.Tools;
using Syncfusion.Windows.Forms.Tools.XPMenus;

namespace ctsu.BaseForms
{
	public class FormBase : System.Windows.Forms.Form
	{
		public IFormBaseController Controller;
		public int CurrentPrimaryID;
		public object DialogReturnObject;
		//property
		private bool _centerBottomButtons = true;
		private int _minPanel2Width = 0;
		private int _minPanel2Height = 0;

		private Scroll scroll;
		private System.Windows.Forms.Panel pnl1;
		private System.Windows.Forms.HScrollBar hScroll;
		private System.Windows.Forms.VScrollBar vScroll;
		protected System.Windows.Forms.GroupBox groupBox1;

		public System.Windows.Forms.Panel pnl2;
		public System.Windows.Forms.Button btnExit;
		public System.Windows.Forms.Button btnCancel;
		public System.Windows.Forms.Button btnSave;
		public System.Windows.Forms.Button btnNew;
		public System.Windows.Forms.Button btnRemove;
		public System.Windows.Forms.Button btnEdit;
		public System.Windows.Forms.Button btnSearch;
		public System.Windows.Forms.Button btnGrid;

		public System.Windows.Forms.TextBox Mode;
		public System.Windows.Forms.ErrorProvider errP;
		public System.Windows.Forms.ToolTip tip;
		public System.Windows.Forms.CheckBox chkGridTracking;

		public System.Windows.Forms.Label label1;
		public System.Windows.Forms.CheckBox chkCarry;

		private System.ComponentModel.IContainer components;

		//public properties
		[Browsable(true)]
		public int MinPanel2Width
		{
			get { return _minPanel2Width; }
			set { _minPanel2Width = value; }
		}

		[Browsable(true)]
		public int MinPanel2Height
		{
			get { return _minPanel2Height; }
			set { _minPanel2Height = value; }
		}

		[Browsable(true)]
		public bool CenterBottomButtons
		{
			get { return _centerBottomButtons; }
			set { _centerBottomButtons = value; }
		}

		public FormBase()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			//initialize the scroll bar functionality
			scroll = new Scroll(pnl2.Height, pnl2.Width, pnl1, pnl2, vScroll, hScroll);
			this.hScroll.ValueChanged += new System.EventHandler(scroll.hScroll_ValueChanged);
			this.vScroll.ValueChanged += new System.EventHandler(scroll.vScroll_ValueChanged);
			if (this.DesignMode)
			{
				pnl2.AutoScroll = true;
				hScroll.Visible = false;
				vScroll.Visible = false;
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
					components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormBase));
			this.pnl1 = new System.Windows.Forms.Panel();
			this.hScroll = new System.Windows.Forms.HScrollBar();
			this.vScroll = new System.Windows.Forms.VScrollBar();
			this.pnl2 = new System.Windows.Forms.Panel();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.chkCarry = new System.Windows.Forms.CheckBox();
			this.chkGridTracking = new System.Windows.Forms.CheckBox();
			this.btnGrid = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.Mode = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnNew = new System.Windows.Forms.Button();
			this.btnRemove = new System.Windows.Forms.Button();
			this.btnEdit = new System.Windows.Forms.Button();
			this.btnSearch = new System.Windows.Forms.Button();
			this.tip = new System.Windows.Forms.ToolTip(this.components);
			this.errP = new System.Windows.Forms.ErrorProvider(this.components);
			this.pnl1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.errP)).BeginInit();
			this.SuspendLayout();
			// 
			// pnl1
			// 
			this.pnl1.Controls.Add(this.hScroll);
			this.pnl1.Controls.Add(this.vScroll);
			this.pnl1.Controls.Add(this.pnl2);
			this.pnl1.Location = new System.Drawing.Point(0, 46);
			this.pnl1.Name = "pnl1";
			this.pnl1.Size = new System.Drawing.Size(672, 229);
			this.pnl1.TabIndex = 3;
			this.pnl1.Resize += new System.EventHandler(this.pnl1_Resize);
			// 
			// hScroll
			// 
			this.hScroll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.hScroll.LargeChange = 201;
			this.hScroll.Location = new System.Drawing.Point(0, 213);
			this.hScroll.Maximum = 200;
			this.hScroll.Name = "hScroll";
			this.hScroll.Size = new System.Drawing.Size(8, 16);
			this.hScroll.TabIndex = 2;
			this.hScroll.Visible = false;
			// 
			// vScroll
			// 
			this.vScroll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.vScroll.LargeChange = 201;
			this.vScroll.Location = new System.Drawing.Point(656, 0);
			this.vScroll.Maximum = 200;
			this.vScroll.Name = "vScroll";
			this.vScroll.Size = new System.Drawing.Size(16, 8);
			this.vScroll.TabIndex = 1;
			this.vScroll.Visible = false;
			// 
			// pnl2
			// 
			this.pnl2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pnl2.Location = new System.Drawing.Point(0, 0);
			this.pnl2.Name = "pnl2";
			this.pnl2.Size = new System.Drawing.Size(656, 216);
			this.pnl2.TabIndex = 0;
			this.pnl2.Resize += new System.EventHandler(this.pnl2_Resize);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.chkCarry);
			this.groupBox1.Controls.Add(this.chkGridTracking);
			this.groupBox1.Controls.Add(this.btnGrid);
			this.groupBox1.Controls.Add(this.btnExit);
			this.groupBox1.Controls.Add(this.Mode);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.btnCancel);
			this.groupBox1.Controls.Add(this.btnSave);
			this.groupBox1.Controls.Add(this.btnNew);
			this.groupBox1.Controls.Add(this.btnRemove);
			this.groupBox1.Controls.Add(this.btnEdit);
			this.groupBox1.Controls.Add(this.btnSearch);
			this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
			this.groupBox1.Location = new System.Drawing.Point(0, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(704, 40);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			// 
			// chkCarry
			// 
			this.chkCarry.Location = new System.Drawing.Point(582, 14);
			this.chkCarry.Name = "chkCarry";
			this.chkCarry.Size = new System.Drawing.Size(112, 19);
			this.chkCarry.TabIndex = 29;
			this.chkCarry.Text = "Carry To New:";
			this.tip.SetToolTip(this.chkCarry, "Carrry current data to new record.");
			this.chkCarry.CheckedChanged += new System.EventHandler(this.chkCarry_CheckedChanged);
			// 
			// chkGridTracking
			// 
			this.chkGridTracking.Location = new System.Drawing.Point(452, 14);
			this.chkGridTracking.Name = "chkGridTracking";
			this.chkGridTracking.Size = new System.Drawing.Size(131, 19);
			this.chkGridTracking.TabIndex = 28;
			this.chkGridTracking.Text = "Record Tracking:";
			this.chkGridTracking.CheckedChanged += new System.EventHandler(this.chkGridTracking_CheckedChanged);
			// 
			// btnGrid
			// 
			this.btnGrid.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnGrid.Image = ((System.Drawing.Image)(resources.GetObject("btnGrid.Image")));
			this.btnGrid.Location = new System.Drawing.Point(408, 11);
			this.btnGrid.Name = "btnGrid";
			this.btnGrid.Size = new System.Drawing.Size(32, 23);
			this.btnGrid.TabIndex = 25;
			this.tip.SetToolTip(this.btnGrid, "View Table");
			this.btnGrid.Click += new System.EventHandler(this.btnGrid_Click);
			// 
			// btnExit
			// 
			this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnExit.Image = ((System.Drawing.Image)(resources.GetObject("btnExit.Image")));
			this.btnExit.Location = new System.Drawing.Point(266, 11);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(32, 23);
			this.btnExit.TabIndex = 24;
			this.tip.SetToolTip(this.btnExit, "Exit");
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// Mode
			// 
			this.Mode.BackColor = System.Drawing.Color.DarkBlue;
			this.Mode.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.Mode.ForeColor = System.Drawing.Color.White;
			this.Mode.Location = new System.Drawing.Point(352, 13);
			this.Mode.Name = "Mode";
			this.Mode.ReadOnly = true;
			this.Mode.Size = new System.Drawing.Size(48, 20);
			this.Mode.TabIndex = 23;
			this.Mode.TabStop = false;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(304, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 22;
			this.label1.Text = "Mode:";
			// 
			// btnCancel
			// 
			this.btnCancel.Enabled = false;
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnCancel.Image = ((System.Drawing.Image)(resources.GetObject("btnCancel.Image")));
			this.btnCancel.Location = new System.Drawing.Point(226, 11);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(32, 23);
			this.btnCancel.TabIndex = 21;
			this.tip.SetToolTip(this.btnCancel, "Cancel");
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnSave
			// 
			this.btnSave.Enabled = false;
			this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnSave.Image = ((System.Drawing.Image)(resources.GetObject("btnSave.Image")));
			this.btnSave.Location = new System.Drawing.Point(186, 11);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(32, 23);
			this.btnSave.TabIndex = 20;
			this.tip.SetToolTip(this.btnSave, "Save");
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnNew
			// 
			this.btnNew.Enabled = false;
			this.btnNew.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnNew.Image = ((System.Drawing.Image)(resources.GetObject("btnNew.Image")));
			this.btnNew.Location = new System.Drawing.Point(50, 11);
			this.btnNew.Name = "btnNew";
			this.btnNew.Size = new System.Drawing.Size(32, 23);
			this.btnNew.TabIndex = 17;
			this.tip.SetToolTip(this.btnNew, "New");
			this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
			// 
			// btnRemove
			// 
			this.btnRemove.Enabled = false;
			this.btnRemove.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnRemove.Image = ((System.Drawing.Image)(resources.GetObject("btnRemove.Image")));
			this.btnRemove.Location = new System.Drawing.Point(130, 11);
			this.btnRemove.Name = "btnRemove";
			this.btnRemove.Size = new System.Drawing.Size(32, 23);
			this.btnRemove.TabIndex = 19;
			this.tip.SetToolTip(this.btnRemove, "Remove");
			this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
			// 
			// btnEdit
			// 
			this.btnEdit.Enabled = false;
			this.btnEdit.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnEdit.Image = ((System.Drawing.Image)(resources.GetObject("btnEdit.Image")));
			this.btnEdit.Location = new System.Drawing.Point(90, 11);
			this.btnEdit.Name = "btnEdit";
			this.btnEdit.Size = new System.Drawing.Size(32, 23);
			this.btnEdit.TabIndex = 18;
			this.tip.SetToolTip(this.btnEdit, "Edit");
			this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
			// 
			// btnSearch
			// 
			this.btnSearch.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnSearch.Image = ((System.Drawing.Image)(resources.GetObject("btnSearch.Image")));
			this.btnSearch.Location = new System.Drawing.Point(10, 11);
			this.btnSearch.Name = "btnSearch";
			this.btnSearch.Size = new System.Drawing.Size(32, 23);
			this.btnSearch.TabIndex = 16;
			this.tip.SetToolTip(this.btnSearch, "Search");
			this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
			// 
			// errP
			// 
			this.errP.ContainerControl = this;
			// 
			// FormBase
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(704, 302);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.pnl1);
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.MinimumSize = new System.Drawing.Size(304, 304);
			this.Name = "FormBase";
			this.Resize += new System.EventHandler(this.FormBase_Resize);
			this.Closing += new System.ComponentModel.CancelEventHandler(this.FormBase_Closing);
			this.Load += new System.EventHandler(this.FormBase_Load);
			this.pnl1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.groupBox1.PerformLayout();
			((System.ComponentModel.ISupportInitialize)(this.errP)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void pnl1_Resize(object sender, System.EventArgs e)
		{
			if (!this.DesignMode)
				scroll.CalculateScrolls();

		}

		private void pnl2_Resize(object sender, System.EventArgs e)
		{
			if (!this.DesignMode)
			{
				scroll.MaximumHorizontal = pnl2.Width;
				scroll.MaximumVertical = pnl2.Height;
			}
			else
				pnl1.AutoScroll = true;

		}

		private void FormBase_Load(object sender, System.EventArgs e)
		{
			Console.WriteLine("FormBase_Load");
			scroll.CalculateScrolls();

		}

		private void FormBase_Resize(object sender, System.EventArgs e)
		{
			Console.WriteLine("FormBase_Resize");
			this.pnl1.Height = this.Height - 45;
			this.pnl1.Width = this.Width - 8;
			//			if(! this.DesignMode)
			//			{
			//				if(this.pnl2.Height < this._minPanel2Height)
			//					this.pnl2.Height = this._minPanel2Height;
			//				else
			//					this.pnl2.Height = pnl1.Height-50;
			//
			//				if(this.pnl2.Width < this._minPanel2Width)
			//					this.pnl2.Width = this._minPanel2Width;
			//				else
			//					this.pnl2.Width = pnl1.Width-25;
			//			}
			//			if(this.CenterBottomButtons)
			//			{
			//				int left = (int)((this.Width-176)/2)-4;
			//				int bottom = this.Height-30; //- 56;
			//			
			//				btnSave.Location = new Point(left, bottom);
			//				btnCancel.Location = new Point(btnSave.Left + 96, bottom);
			//			}
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if (Controller != null)
				Controller.Accept(sender, e);
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			if (Controller != null)
				Controller.Cancel(sender, e);
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			if (Controller != null)
				Controller.Exit(sender, e);
		}

		private void btnSearch_Click(object sender, System.EventArgs e)
		{
			if (Controller != null)
				Controller.Search(sender, e);
		}

		private void btnNew_Click(object sender, System.EventArgs e)
		{
			if (Controller != null)
				Controller.New(sender, e);
		}

		private void btnEdit_Click(object sender, System.EventArgs e)
		{
			if (Controller != null)
				Controller.Edit(sender, e);
		}

		private void btnRemove_Click(object sender, System.EventArgs e)
		{
			if (Controller != null)
				Controller.Remove(sender, e);
		}

		private void FormBase_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (Controller != null)
				Controller.Closing(sender, e);
		}

		private void btnGrid_Click(object sender, System.EventArgs e)
		{
			if (Controller != null)
				Controller.ShowGrid(sender, e);
		}

		private void chkCarry_CheckedChanged(object sender, System.EventArgs e)
		{
			if (Controller != null)
				Controller.CarryNewClicked(sender, e);
		}

		private void chkGridTracking_CheckedChanged(object sender, System.EventArgs e)
		{

		}
	}

	public class GroupViewItemEx : GroupViewItem
	{
		public FormInfoBase FormInfo;
		public GroupViewItemEx(FormInfoBase formInfo, string text)
		{
			this.FormInfo = formInfo;
			base.Text = text;
		}
	}

	public class BarItemEx : BarItem
	{
		public FormInfoBase FormInfo;
		public BarItemEx(FormInfoBase formInfo, string text)
			: base(text)
		{
			this.FormInfo = formInfo;
		}
	}

	public class MenuItemEx : MenuItem
	{
		public FormInfoBase FormInfo;
		public MenuItemEx(FormInfoBase formInfo, string text)
			: base(text)
		{
			this.FormInfo = formInfo;
		}
	}
}
